<?php

/* 
 * This class handles the retrieval of a user's data, specifically: id, email,
 * registration date, number of vineyards visited, which vineyards, reviews and
 * ratings, and achievement level.  In addition to gathering user data, this 
 * class also contains methods to generate the HTML/CSS to display this data.
 */
class StatusHandler
{
  /* Variable holds the database connection */
  private $dbc;
  
  /* User-related instance variables */
  private $id;
  private $email;
  private $dateRegistered;
  private $monthsRegistered;
  private $vineyardCount;
  private $achievementId; 
  private $achievementTitle;
  private $achievementSummary;
  private $reviews;

  /* 
   * Assumes user is logged in and session data exists. (A check will be
   * be performed before the creation of a statusHandler object).
   * The constructor loads data from the session and DB into the instance
   * variables.
   */
  function __construct($dbc)
  {
    $this->dbc = $dbc;

    $this->email = $_SESSION['email']; 
    
    $this->getUserData();
    $this->getMonthsRegistered();
    $this->getVineyardCount();
    $this->getAchievementLevel();
    $this->getAllReviews();
  }
  
  /* Queries the database to get a user's id and registration date */
  private function getUserData()
  {
    $q = "SELECT id, date_created FROM User WHERE email='"
          . $this->email . "';";

    $r = mysqli_query($this->dbc, $q);
    $row = mysqli_fetch_row($r);

    $this->id = $row[0];
    $this->dateRegistered = $row[1];
  }
  
  /* Calculates how many months a user has been a member */
  private function getMonthsRegistered()
  {
    /* 
     * Truncates hours:minutes:seconds off of the time stamp.  Not necessary
     * to have the much precision in calculating months.
     */
    $this->dateRegistered = new DateTime(substr($this->dateRegistered, 0, 10));

    /* Get current time in the same format as the registration time */
    $currentDate = new DateTime();
    $currentDate->format('Y-m-d');
    
    /* Get the difference in months between those two times */
    $this->monthsRegistered = $this->dateRegistered->diff($currentDate)->m;
  }
  
  /* Gets number of vineyards visited. A review/rating is equal to a visit. */
  private function getVineyardCount()
  {
     $q = "SELECT COUNT(*) FROM VineyardChecklist WHERE user_id=" 
          . $this->id . ";";

     $r = mysqli_query($this->dbc, $q);
     $row = mysqli_fetch_row($r);
    
     $this->vineyardCount = $row[0];
  }

  /* Gets the appropriate achievement level for the user */
  private function getAchievementLevel()
  {
    $q = "SELECT id, title, summary FROM AchievementList WHERE 
          vineyards_visited <= " . $this->vineyardCount . " AND
          months_registered <= " . $this->monthsRegistered . " ORDER BY id
          DESC LIMIT 1";

    $r = mysqli_query($this->dbc, $q);
    $row = mysqli_fetch_row($r);

    $this->achievementId = $row[0];
    $this->achievementTitle = $row[1];
    $this->achievementSummary = $row[2];
  }
  
  /* Gets all vineyard reviews that the user has made */
  private function getAllReviews()
  {
    $q = "SELECT v.name, r.comment, r.review, r.vineyard_id FROM Review AS r 
          INNER JOIN Vineyard as v WHERE r.vineyard_id = v.id 
          AND user_id = " . $this->id . ";";

    $r = mysqli_query($this->dbc, $q);

    while($row = mysqli_fetch_row($r))
    {
      $this->reviews[] = $row;  
    }
  }
  
  /* Generates the user details gather in HTML */
  public function displayUserDetails()
  {
    echo '<h3>' . $this->email . '</h3>';
    echo '<p>Date registered: <b>' . $this->dateRegistered->format('M d, Y') . '</b></p>';
    echo '<p>Number of vineyards visited: <b>' . $this->vineyardCount . '</b></p>';
    echo '<p> Level: <b>' . $this->achievementTitle;
    echo '</b> -- <b>' . $this->achievementSummary . '</b></p>';
  }
  
  /* Generates the HTML for all the user's reviews */
  public function displayReviews()
  {
    if(empty($this->reviews))
    {
      echo '<p>No reviews yet!</p>';
    }
    else
    {
      foreach($this->reviews as $review)
      {
        echo '<div id="single_review">';
        echo '<a href="vineyard.php?vineyard=' . $review[3] . '">' . $review[0] . '</a>'; 
        echo '<p><em>' . $review[1] . '</em></p>';
        echo '<p class="rating">Rating: ' . $review[2] . '</p>';
        echo '</div>';
      }
    }
  }
}
?>
